<!-- 用于弹出框来选择楼栋信息的页面，通过上传owner字段来判断到底是显示业主选项还是设备选项 -->

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/taglib/c.tld" prefix="c"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> 

<%
String path = request.getContextPath();
String strPraId =(String)request.getParameter("menuPare");
String strChlId =(String)request.getParameter("menuChild");
String showCommCode =(String)request.getParameter("showCommCode");
%>

<!DOCTYPE html>
<html>
  <head>
    <title>添加楼栋单元</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <link rel="stylesheet" type="text/css" href="<%=path %>/plugins/layui/css/layui.css">
	<script src="<%=path %>/plugins/layui/layui.js"></script>
	<script src="<%=path %>/plugins/jquery.min.js" type="text/javascript"></script>
	<script type="text/javascript" src="<%=path%>/js/buildingSelect.js?version=1"></script>
	<script type="text/javascript" src="<%=path%>/js/core/helper.js"></script>
	
	<SCRIPT language="javascript">
		var layer;
		var index = parent.layer.getFrameIndex(window.name);
		layui.use(['jquery','layer','form'], function(args){
  		 layer = layui.layer; 
  		 
    	commCodeStr = '${commDimBo.commCode}';
    	
  		setCommSelect("commCode");
		setAreaSelect("areaCode");
		setBuildingSelect("buildingCode");
		if("${owner}" == 'true'){
			setUnitSelect("unitCode");
			setRoomSelect("roomId");
			setOwnerSelect("ownerCode");
		}else{
			setDevSelect("dev");
		}

		setUrl('<%=path%>/BuildingsCtrl/getUnitsInfo.x');
		setOwnerUrl('<%=path%>/owner/findOwnerByRoom.x');
		if (commCodeStr != null && commCodeStr != '')
		{
			getSelectData(commCodeStr);
		}
		}); 
		
		/* 将设备号转换为IC卡权限设备号 ,由于IC卡内部使用2个字节表示一个设备号,因此将16个字节拆分成3个区域 
			栋数  - 单元号 - 设备号 
			9字节      4字节        3字节 
			
			因此可以支持512栋， 每栋16单元，每单元8个设备 .
			围墙机号码,使用栋数单元数为0即可 
		*/
		function getDevNumber(){
			var areaCodeInt = parseInt($("select[name='"+areaSelect+"']").val());
			var buildingCodeInt = parseInt($("select[name='"+buildingSelect+"']").val());
			var devNumInt = parseInt($("select[name='"+devSelect+"'] option:selected").attr("name"));
			
			var result = (areaCodeInt << 7) | (buildingCodeInt << 3) | devNumInt;
			var resultStr = result.toString(16);
			
			if(resultStr.length == 2) 
				resultStr = "00"+resultStr;
			else if(resultStr.length == 3)
				resultStr = "0"+resultStr;
			
			return resultStr;
		}
		
		function getDefaultPerms(ownerCode){
			var param = {};
	    	param.ownerCode = ownerCode;
			postJson("<%=path%>/card/getDefaultPerms.x",$.param(param), function(data){	
				var recvData = data.data;
				for(var i=0; i<recvData.length; i++) {
					parent.callbackDlgAddPerms(recvData[i].commCode,recvData[i].devStr,recvData[i].devNo,recvData[i].devBuildingNo);
				}
	    	});
		}
		
		function select(){	
			if("${owner}" == 'true' && $("select[name='ownerCode']").val() == ''){
				parent.layer.alert("未选择业主");
				return;
			}
			
			/* 业主选择器确认按钮 */
			if("${owner}" == 'true'){
				/* 获取业主文本信息,传给父窗口控件 */
				var owner = '['+$("select[name='commCode'] option:selected").text()+'] ' + $("select[name='areaCode']").val()+"栋"+ $("select[name='buildingCode']").val() + "单元" +
					$("select[name='unitCode']").val() + $("select[name='roomId']").val() + " "+$("select[name='ownerCode'] option:selected").text();
				parent.$("#selectOwner").text(owner);
				console.log("roomName1 "+parent.$("#selectOwner").text());
				/* 将业主编号，小区编号传给父窗口 */
				var ownerCode = $("select[name='ownerCode'] option:selected").val();
				var ownerName = $("select[name='ownerCode'] option:selected").text();
				var commCode = $("select[name='commCode'] option:selected").val();
				var roomName = $("select[name='commCode']").val() + $("select[name='areaCode']").val() + 
					$("select[name='buildingCode']").val() + $("select[name='unitCode']").val() +$("select[name='roomId']").val();
				parent.$("#ownerCode").val(ownerCode);
				parent.$("#commCode").val(commCode);
				parent.$("#ownerName").val(ownerName);
				parent.$("#roomName").val(roomName);

				parent.clearPerms(); //显示权限前先清除原有显示，因为选择业主了
				if($("#autoUnitGate").is(":checked")){			
					getDefaultPerms(ownerCode);
				}

				var index = parent.layer.getFrameIndex(window.name); 
				parent.layer.close(index);
			}
			/* 设备选择器确认按钮 */
			else{
				/* 选择设备后, 添加设备信息和删除按钮 */
				var commName = $("select[name='commCode'] option:selected").text();
				var devDisp = "["+commName+"] " + $("select[name='areaCode']").val()+"栋"+ $("select[name='buildingCode']").val() + "单元" +
				$("select[name='dev'] option:selected").text() + "号机";
				var devNumber = getDevNumber();
				var devAccount = $("select[name='dev'] option:selected").val();
				var commCode = $("select[name='commCode']").val();
				parent.callbackDlgAddPerms(commCode,devDisp,devNumber,devAccount);
				var index = parent.layer.getFrameIndex(window.name); 
				parent.layer.close(index);
			}
		}
	</SCRIPT>
  </head>
  
  <body>
<div id="selectOwner" style="margin:20px; width:900px">
	<div>
		<c:if test="${!empty commDimList}">
		小区:
		<select name="commCode" onchange="changeComm(this);">
			<option value="">--请选择--</option>
			<c:forEach var="comm" items="${commDimList}">
				<option value='${comm.commCode }' 
					<c:if test="${comm.commCode== TblOwnerDimForm.commCode}"> selected="selected"</c:if>>
					${comm.commCode }-${comm.commName}
				</option>
			</c:forEach>
		</select>
		</c:if>
		<c:if test="${!empty commDimBo}">
		    	<select name="commCode" style="display:none">
		    		<option value="${commDimBo.commCode}" selected>${commDimBo.commName }</option>
		    	</select>
		    </c:if>
		<c:if test="${empty commDimList && empty commDimBo}">
		小区:
			<select name="commCode" onchange="changeComm(this);">
				<option value="">--没有小区可选择--</option>
			</select>
		</c:if>
					           		 
		栋数:<select name="areaCode" onchange="changeArea();">
		<c:if test="${empty TblOwnerDimForm.areaCode}">
			<option value="">--请选择--</option>
		</c:if>
		</select>
		<c:if test="${owner == 'true'}">
		单元:<select name="buildingCode" onchange="changeBuilding();">
		</c:if>
		<c:if test="${owner != 'true'}">
		单元:<select name="buildingCode" onchange="changeBuildingDev();">
		</c:if>
			<c:if test="${empty TblOwnerDimForm.buildingCode}">
				<option value="">--请选择--</option>
			</c:if>
		</select>
		
		<c:if test="${owner != 'true'}">
		设备号:<select name="dev">
				<option value="">--请选择--</option>
			 </select>
		</c:if>
		<c:if test="${owner == 'true'}">
		 楼宇:<select name="unitCode">
			<c:if test="${empty TblOwnerDimForm.unitCode}">
				<option value="">--请选择--</option>
			</c:if>
		</select>
		房号:<select name="roomId" onchange="changeRoomOwner();">
			<c:if test="${empty TblOwnerDimForm.roomId}">
				<option value="">--请选择--</option>
			</c:if>
		</select>   
			业主<select name="ownerCode">
				<option value="">--请选择业主--</option>
			</select>
		</c:if>
		<c:if test="${owner != 'true'}">
			<button type="button" class="layui-btn layui-btn-small" style="margin-left:10px" onclick="select()">确定</button>
		</c:if>
	</div>
	<c:if test="${owner == 'true'}">
		<hr/>
		<div style="margin-left:150px">
			自动添加单元门与围墙机权限<input id="autoUnitGate" type="checkbox" name="autoUnit" checked/>&nbsp&nbsp&nbsp&nbsp&nbsp
			<button type="button" class="layui-btn layui-btn-small" style="margin-left:10px" onclick="select()">确定</button>
		</div>
		</div> 
	</c:if>
	
	</body>
</html>
